#--------------------------------------------------------------------------------------------------------------------#
#----------------------------------------------- Portmann & Stojanovic ----------------------------------------------#
#--------------------------------------------------- November 2020 --------------------------------------------------#
#------- Are Immigrant-Origin Candidates Penalized in Virtue of Ingroup Favoritism or Outrgoup Hostility ? ----------#
#--------------------------------------------------------------------------------------------------------------------#
#--------------------------------------------------------- Main -----------------------------------------------------#
#--------------------------------------------------------------------------------------------------------------------#


#-------------- PACKAGES

want = c("dplyr", "plyr", "Hmisc", "ggplot2", "gamlss", "ggeffects", "dplyr", "sjPlot", "sjmisc",
         "lme4", "effects", "texreg", "readstata13", "grid", "reshape2", "gridExtra", "scales", "xtable",
         "haven", "foreign", "xtable", "ggpubr", "lme4", "nlme", "statmod", "car", "gamlss", 
         "gamlss", "knitr", "xlsx")
have = want %in% rownames(installed.packages())
if ( any(!have) ) { install.packages( want[!have] ) }

# load packages
junk <- lapply(want, library, character.only = TRUE)
rm(have,want,junk)

#-------------- FUNCTIONS

#--- VIF to check multicollinearity

vif.lme <- function (fit) {
  ## adapted from rms::vif
  v <- vcov(fit)
  nam <- names(fixef(fit))
  ## exclude intercepts
  ns <- sum(1 * (nam == "Intercept" | nam == "(Intercept)"))
  if (ns > 0) {
    v <- v[-(1:ns), -(1:ns), drop = FALSE]
    nam <- nam[-(1:ns)] }
  d <- diag(v)^0.5
  v <- diag(solve(v/(d %o% d)))
  names(v) <- nam
  v }

